package com.zhkj.shixi.tvplayer.MusicPlayer;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.zhkj.shixi.tvplayer.R;
import com.zhkj.shixi.tvplayer.VideoPlayer.VideoPlayOrder;
import com.zhkj.shixi.tvplayer.adapter.AudioAdapter;
import com.zhkj.shixi.tvplayer.adapter.PicBgAdapter;
import com.zhkj.shixi.tvplayer.adapter.PlayList;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenhao on 19-8-6.
 */

public class AudioUI implements View.OnClickListener,OnSeekBarChangeListener {
    private static final String TAG = "AudioUI";

    private Activity context;
    private ImageButton mBtnList;
    private ImageButton mBtnOrderMode;
    private ImageButton mBtnAllMode;
    private ImageButton mBtnRandMode;
    private ImageButton mBtnRepeatMode;
    private ImageButton mBtnPlay;
    private ImageButton mBtnPre;
    private ImageButton mBtnNext;
    private ImageButton mBtnBgImage;
    private SeekBar mSeekBar;
    private CircleImageView mPostImage;
    private ListView mMusicList;
    private ListView mImageList;
    private View mPlayListShow;
    private View mImageListShow;
    private Boolean mIsplaying=true;
    private PlayerBackGround playerBg;


    public AudioUI(Activity activity){
        context =activity;
        init();
    }
    public void init(){
        mBtnList =context.findViewById(R.id.btn_list);
        mBtnOrderMode =context.findViewById(R.id.btn_ordermode);
        mBtnAllMode =context.findViewById(R.id.btn_allmode);
        mBtnRandMode=context.findViewById(R.id.btn_randmode);
        mBtnRepeatMode=context.findViewById(R.id.btn_repeatmode);
        mBtnPlay =context.findViewById(R.id.btn_play);
        mMusicList =context.findViewById(R.id.play_list);
        mBtnPre =context.findViewById(R.id.btn_last);
        mBtnNext = context.findViewById(R.id.btn_next);
        mBtnBgImage =context.findViewById(R.id.btn_bg);
        mImageList =context.findViewById(R.id.image_list);
        mSeekBar =context.findViewById(R.id.playSeekBar);
        mPlayListShow=context.findViewById(R.id.play_listshow);
        mImageListShow = context.findViewById(R.id.image_listshow);
        mBtnPlay.setOnClickListener(this);
        mBtnList.setOnClickListener(this);
        mBtnOrderMode.setOnClickListener(this);
        mBtnAllMode.setOnClickListener(this);
        mBtnRandMode.setOnClickListener(this);
        mSeekBar.setOnSeekBarChangeListener(this);
        mBtnRepeatMode.setOnClickListener(this);
        mBtnPre.setOnClickListener(this);
        mBtnNext.setOnClickListener(this);
        mBtnBgImage.setOnClickListener(this);
        playerBg = new PlayerBackGround(context);
        playerBg.initList();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_play:
                if (mIsplaying){
                    mBtnPlay.setImageResource(R.drawable.player_audio_button_suspendstop_selector);
                    ((AudioActivity) context).getMusicCtrl().pause();
                    Log.i(TAG, "onClick: stop");
                    mIsplaying =false;
                }else {
                    Log.i(TAG, "onClick: play");
                    mBtnPlay.setImageResource(R.drawable.player_audio_button_suspend_selector);
                    ((AudioActivity) context).getMusicCtrl().play();
                    mIsplaying =true;
                }
                break;
            case R.id.btn_list:
                final List<PlayList> musicList=new ArrayList<>();
                final AudioActivity audioActivity = (AudioActivity)context;
                int i=0;
                Log.i(TAG, "list: "+audioActivity.getmMusicList().size());
                for(File file : audioActivity.getmMusicList()) {
                    PlayList f = new PlayList(file.getName());
                    musicList.add(f);
                    Log.i(TAG, "onClick: "+file.getName());
                    i++;
                }
                Log.i(TAG, "onClick: "+musicList);

                AudioAdapter audioAdapter = new AudioAdapter(context,R.layout.item_audioplaylist,R.id.item_audiolist,musicList);
                mMusicList.setAdapter(audioAdapter);
                mMusicList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        MusicCtrl musicCtrl = ((AudioActivity) context).getMusicCtrl();
                        ((AudioActivity) context).setNowFile(audioActivity.getmMusicList().get(position));
                        musicCtrl.initPlay();
                        musicCtrl.play();
                        setInvisiable(mPlayListShow);
                    }
                });
                setInvisiable(mImageListShow);
                mPlayListShow.setVisibility(View.VISIBLE);
                mMusicList.requestFocus();
                break;
            case R.id.btn_bg:
                PicBgAdapter picBgAdapter = new PicBgAdapter(context,R.layout.item_audioimagelist,R.id.item_imagelist,playerBg.getPicList());
                mImageList.setAdapter(picBgAdapter);
                mImageList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        PlayerBackGround.Pic pic = playerBg.getPicList().get(position);
                        String path = pic.path;
                        Drawable drawable = Drawable.createFromPath(path);
                        View contentView = context.findViewById(R.id.audioActivity);
                        contentView.setBackground(drawable);
                    }
                });
                setInvisiable(mPlayListShow);
                mImageListShow.setVisibility(View.VISIBLE);

                break;

            case R.id.btn_next:
                ((AudioActivity) context).getMusicCtrl().next();
                upgradeUiOnChangeFile();
                break;
            case R.id.btn_last:
                ((AudioActivity) context).getMusicCtrl().prev();
                upgradeUiOnChangeFile();
                break;
            case R.id.btn_allmode:
                setInvisiable(mBtnAllMode);
                setVisiable(mBtnRandMode);
                mBtnRandMode.requestFocus();
                // TODO: 切换为随机播放
                ((AudioActivity) context).getmPlayOrder().setType(VideoPlayOrder.RAND);
                Log.i(TAG, "onClick: "+((AudioActivity) context).getmPlayOrder().getType());
                break;
            case R.id.btn_randmode:
                setInvisiable(mBtnRandMode);
                setVisiable(mBtnOrderMode);
                mBtnOrderMode.requestFocus();
                // TODO: 切换为顺序播放
                ((AudioActivity) context).getmPlayOrder().setType(VideoPlayOrder.ORDER);
                Log.i(TAG, "onClick: "+((AudioActivity) context).getmPlayOrder().getType());
                break;
            case R.id.btn_repeatmode:
                setInvisiable(mBtnRepeatMode);
                setVisiable(mBtnAllMode);
                mBtnAllMode.requestFocus();
                // TODO: 切换为列表循环
                ((AudioActivity) context).getmPlayOrder().setType(VideoPlayOrder.CYCLE);
                Log.i(TAG, "onClick: "+((AudioActivity) context).getmPlayOrder().getType());
                break;
            case R.id.btn_ordermode:
                setInvisiable(mBtnOrderMode);
                setVisiable(mBtnRepeatMode);
                mBtnRepeatMode.requestFocus();
                // TODO: 切换为单曲循环
                ((AudioActivity) context).getmPlayOrder().setType(VideoPlayOrder.SINGLE);
                Log.i(TAG, "onClick: "+((AudioActivity) context).getmPlayOrder().getType());
                break;
            default:
                break;
        }
    }
    /**
     * 替换海报
     * @param bitmap
     */
    public void mp3bg(Bitmap bitmap){
        mPostImage = context.findViewById(R.id.imagecover);
//        mPostImage.setImageResource(R.drawable.player_audio_down_focus);
        mPostImage.setImageBitmap(bitmap);
    }
    public void setVisiable(View view){
        view.setVisibility(view.VISIBLE);
    }
    public void setInvisiable(View view){
        view.setVisibility(view.GONE);
    }

    /**设置进度条长度为音乐长度
     *
     * @param musiclength
     */
    public void setSeekBarMax(int musiclength){
        mSeekBar.setMax(musiclength);
    }
    public void setSeekBarProgress(int currentPosition){
        mSeekBar.setProgress(currentPosition);
    }

    /**
     * 在切换文件时更新UI
     */
    public void upgradeUiOnChangeFile() {
        setSeekBarMax(((AudioActivity) context).getMusicCtrl().getDuration() / 1000);
        ((AudioActivity) context).getMusicCtrl().upgradeSeekBar();
        //TODO 其他更新UI操作
        mp3bg(((AudioActivity) context).getMP3Poster());//海报更新

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        // TODO: 19-8-8 音乐播放器进度条监听
        if (fromUser) {
            ((AudioActivity) context).getMusicCtrl().changePosition(mSeekBar.getProgress());
        }
    }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}